<template>
  <div class="movie-card">
    <div class="movie-poster">
        <img
      :src="movie.picPaths"
      :alt="props.movie.title"
    />
    
      <!-- @error="handleImageError" -->
    </div>
    <div class="movie-info">
      <h3>{{ props.movie.title ? props.movie.title : "暂无标题" }}</h3>
      <p class="movie-year">{{ props.movie.year }}</p>
      <div class="movie-rating">
        <span class="stars">★★★★★</span>
        <span class="rating">{{ props.movie.rating }}</span>
      </div>
      <p class="movie-genre">{{ props.movie.genre }}</p>
      <p class="movie-author">发布者: {{ props.movie.authorName }}</p>
      <p class="movie-added-time">发布时间: {{ props.movie.addedTime }}</p>
      <p class="movie-added-time">商品价格: <span class="red">{{ props.movie.price }}元</span></p>

      <p class="movie-body">商品描述: {{ props.movie.desc }}</p>
      <div class="movie-actions">
        <button class="btn btn-primary" @click="viewMovie">查看商品详情</button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue';
import { useRouter } from 'vue-router';
import { useUserStore } from '@/stores/user';
const imageError = ref(false);

function handleImageError(e) {
  e.target.src = "@/assets/images/bg.png"; // 确保路径正确
}
const store = useUserStore();
const router = useRouter();

const props = defineProps({
  movie: {
    type: Object,
    required: true
  }
});

const emit = defineEmits(['view-movie', 'add-comment']);

const viewMovie = () => {
  emit('view-movie', props.movie.id);
};
</script>

<style scoped>
.movie-info *{
  width: 270px; /* 设置容器的宽度 */
  white-space: nowrap; /* 防止文本换行 */
  overflow: hidden; /* 隐藏溢出的文本 */
  text-overflow: ellipsis; /* 溢出时显示省略号 */
}
.red{
  color: red;
}
.movie-card {
  background-color: white;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s, box-shadow 0.3s;
  margin-bottom: 20px;
  display: flex;
  flex-direction: column;
  max-width: 300px;
}
.movie-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
}
img {
  width: 100%;
  height: auto;
  display: block;
}

img.loading::after {
  content: 'Loading...';
  display: block;
  text-align: center;
  color: #888;
  margin-top: 10px;
}
.movie-poster img {
  width: 100%;
  height: 200px;
  object-fit: cover;
}

.movie-info {
  padding: 15px;
  display: flex;
  flex-direction: column;
}

.movie-info h3 {
  margin: 0 0 8px;
  font-size: 1.2rem;
  color: #1a1a2e;
}

.movie-year {
  color: #666;
  margin: 0 0 8px;
}

.movie-rating {
  display: flex;
  align-items: center;
  margin: 8px 0;
}

.stars {
  color: #ffc107;
  margin-right: 5px;
}

.rating {
  font-weight: bold;
  color: #1a1a2e;
}

.movie-genre {
  color: #666;
  margin-bottom: 12px;
}

.movie-author {
  color: #666;
  margin-bottom: 8px;
}

.movie-added-time {
  color: #666;
  margin-bottom: 8px;
}

.movie-body {
  color: #666;
  margin-bottom: 15px;
  line-height: 1.4;
}

.movie-actions {
  display: flex;
  gap: 8px;
  margin-top: auto;
}

.btn {
  padding: 8px 12px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-weight: 500;
  flex: 1;
  text-align: center;
}

.btn-primary {
  background-color: #e94560;
  color: white;
}

.btn-comment {
  background-color: #f0f0f0;
  color: #333;
}

.btn:hover {
  opacity: 0.9;
}

@media (max-width: 480px) {
  .movie-card {
    max-width: 100%;
  }
}
</style>